package com.exes;

import com.mapper.DocumentDetailMapper;
import com.mapper.GetDetailProgressMapper;
import com.model.DocumentDetail;
import com.model.DocumentInfo;
import com.model.GetDetailProgress;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.dao.DuplicateKeyException;

import java.io.IOException;
import java.util.List;

/**
 * 论文详情页抓取任务
 */
@Slf4j
public class ArticleDetailSummaryRunner implements Runnable{

    private List<DocumentInfo> list;
    private DocumentDetailMapper documentDetailMapper;
    private GetDetailProgressMapper getDetailProgressMapper;

    public ArticleDetailSummaryRunner(List<DocumentInfo> list, DocumentDetailMapper documentDetailMapper, GetDetailProgressMapper getDetailProgressMapper){
       this.list = list;
       this.documentDetailMapper = documentDetailMapper;
       this.getDetailProgressMapper = getDetailProgressMapper;
    }

    @Override
    public void run() {
        OkHttpClient okHttpClient = new OkHttpClient();
        for(DocumentInfo documentInfo : list){
            String link = documentInfo.getDetailLink();
            Request request = new Request.Builder()
                    .url(link)
                    .build();
            try {
                Response response = okHttpClient.newCall(request).execute();
                String responseHtml = response.body().string();
                Document document = Jsoup.parse(responseHtml);
                String title = document.select(".wx-tit > h1:nth-child(1)").text();
                String authors = document.select("#authorpart").text();
                String college = document.select("a.author").text();
                String collegeLink = document.select("a.author").attr("href");
                String digest = document.select("#ChDivSummary").text();
                String keywords = document.select(".keywords").text();
                String fundAssistance = document.select(".funds > a:nth-child(1)").text();
                String doi = document.select("li.top-space:nth-child(1) > p:nth-child(2)").text();
                String album = document.select("li.top-space:nth-child(2) > p:nth-child(2)").text();
                String subject = document.select("li.top-space:nth-child(3) > p:nth-child(2)").text();
                String classNumber = document.select("li.top-space:nth-child(4) > p:nth-child(2)").text();
                String downloadText = document.select(".total-inform > span:nth-child(1)").text();
                String[] split = downloadText.split("：");
                Integer downloadCount = 0;
                if(split.length > 1){
                    downloadCount = Integer.parseInt(split[1]);
                }
                String pageNumberText = document.select(".total-inform > span:nth-child(3)").text();
                String[] split2 = pageNumberText.split("：");
                Integer pageNumber = 0;
                if(split2.length > 1){
                    pageNumber = Integer.parseInt(split2[1]);
                }
                String size = "";
                String sizeText = document.select(".total-inform > span:nth-child(4)").text();
                if(sizeText.indexOf("：") != -1){
                    String[] split1 = sizeText.split("：");
                    if(split1.length > 1){
                        size = split1[1];
                    }
                }
                DocumentDetail documentDetail = DocumentDetail.builder()
                        .title(title)
                        .authors(authors)
                        .college(college)
                        .digest(digest)
                        .keywords(keywords)
                        .fundAssistance(fundAssistance)
                        .doi(doi)
                        .album(album)
                        .subject(subject)
                        .classNumber(classNumber)
                        .downloadCount(downloadCount)
                        .pageNumber(pageNumber)
                        .size(size)
                        .searchKeyword(documentInfo.getSearchKeyword())
                        .sid(documentInfo.getId())
                        .build();
                System.out.println(documentDetail);
                int insert = documentDetailMapper.insert(documentDetail);
                getDetailProgressMapper.insert(GetDetailProgress.builder()
                        .articleSummaryId(documentDetail.getId())
                        .status(true)
                        .build());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DuplicateKeyException e){
                log.warn("数据重复: " + e.getMessage());
            }
        }
    }

}
